package com.example.teamtribe.common.userinfo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.database.TeamRequest;
import com.example.teamtribe.main.club.manage.feedback.apply.AppListModel;
import com.example.teamtribe.utils.Utility;

public class UserInfoPresenter implements UserInfoContract.Presenter {

    private UserInfoContract.View view;

    public UserInfoPresenter(UserInfoContract.View view) {
        this.view = view;
    }

    @Override
    public void delete(int id) {
        view.showProgress();
        UserInfoModel.getInstance().delete(Utility.getToken(), id, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                view.next();
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }

    @Override
    public void response(final TeamRequest request, boolean isAgree) {
        view.showProgress();
        final int result = isAgree ? 1 : 2;
        AppListModel.getInstance().response(Utility.getToken(), request.getRealId(), result, "", new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                request.setResult(result);
                request.saveOrUpdate();
                Intent intent = new Intent();
                intent.putExtra("request", request);
                view.setResult(Activity.RESULT_OK, intent);
                view.hideProgress();
                view.setResultInfo(result);
            }

            @Override
            public void failure(String meg) {
                view.hideProgress();
                view.showToast(meg);
            }
        });
    }
}
